import React, {Component} from 'react'
import { Map, is } from 'immutable';
import Actions from '../ActionStore/Actions';
import SerialRuleSetModal from '../../ListSerialrulePage/modal/SerialRuleSetModal'
import {
    Button,
    Input,
    Icon,
    Select,
    Form,
    Row,
    Col,
    Radio,
    message,
    DatePicker,
} from 'antd';
import { fetchData } from '../../../components/AjaxConsumer'
const FormItem = Form.Item;
const Option = Select.Option;
const RadioGroup = Radio.Group;
import moment from 'moment';
const dateFormat = 'YYYY-MM-DD HH:mm:ss'; //DatePicker格式
const formItemLayout = {
    labelCol: {
        span: 6
    },
    wrapperCol: {
        span: 14
    },
};
const buttonItemLayout = {
    wrapperCol: {
        span: 24,
        offset: 9,
    },
};

export default class MainForm extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            isAdd: this.props.isAdd,
            objectInfo: this.props.rowObject
        };
    }

    componentWillReceiveProps(nextProps) {
        if (nextProps.isAdd) {
            this.state = {
                isAdd: nextProps.isAdd,
                objectInfo: {}
            };
        } else {
            this.state = {
                isAdd: nextProps.isAdd,
                objectInfo: nextProps.rowObject
            };
        }
    }

    //提交事件
    saveHandle = (values) => {
        let searchCondition = this.props.searchCondition;//查询条件，创建新数据拼装上查询条件
        let addData = {...values, ...searchCondition}

        const options = {
            url: '/base/product/u',
            type: 'post',
            condition: addData
        };
        fetchData(this, options, (resData) => {
            if (resData.code == "1") {
                message.success('保存成功!');
                Actions.createClick('保存', values);

                this.resetHandle();//添加成功后重置表单
                //this.props.refreshHandle();//添加成功后刷新list列表
            } else {
                message.error("请求数据错误：" + resData.message);
            }
        });
    }

    addHandle = (values) => {
        const options = {
            url: '/base/product/c',
            type: 'post',
            condition: values
        };
        fetchData(this, options, (resData) => {
            if (resData.code == "1") {
                message.success('新增成功!');
                Actions.createClick('新建', values);

                this.resetHandle();//添加成功后重置表单
                this.props.refreshHandle();//添加成功后刷新list列表
            } else {
                message.error("请求数据错误：" + resData.message);
            }
        });
    }

    resetHandle = () => {
        this.props.form.resetFields();
    }

    handleSubmit = (e) => {
        e.preventDefault();
        this.props.form.validateFieldsAndScroll((err, fieldsValue) => {
            if (!err) {


                const values = {//有时间类型的需要再次处理
                    ...fieldsValue
                }
                console.log('form表单值=====', values);
                if (this.state.isAdd) {//新增添加
                    this.addHandle(values);
                } else {//修改保存
                    this.saveHandle(values);
                }
            }
        });
    }

    ruleSearchClick = () => {
        this.refs.serialRuleSetModal.showModal();
    }

    // 入库出库弹出模态框的统一回调函数
    setRuleFormData = (resultData) => {
        this.props.form.setFieldsValue(
            {
                ruleCode: resultData
            }
        );
    }

    render() {
        const { getFieldDecorator, setFieldsValue } = this.props.form;
        let objectInfo = this.state.objectInfo;

        let actionButtons;
        if (this.state.isAdd) {//新增
            actionButtons = (
                <Row className="infoForm_row">
                    <FormItem {...buttonItemLayout}>
                        <Col span={2}>
                            <Button type='primary' htmlType="submit">新增</Button>
                        </Col>
                        <Col span={2}>
                            <Button type='default' onClick={this.resetHandle}>重置</Button>
                        </Col>
                    </FormItem>
                </Row>
            )
        } else {//修改、删除
            actionButtons = (
                <Row className="infoForm_row">
                    <FormItem {...buttonItemLayout}>
                        <Col span={2}>
                            <Button type='primary' htmlType="submit">保存</Button>
                        </Col>
                        <Col span={2}>
                            <Button type='danger' onClick={this.deleteHandle}>删除</Button>
                        </Col>
                        <Col span={2}>
                            <Button type='default' onClick={this.resetHandle}>重置</Button>
                        </Col>
                    </FormItem>
                </Row>
            )
        }
        return (
            <div className="infoForm">
                <Form onSubmit={this.handleSubmit}>
                    <FormItem style={{display: 'none'}}>
                        {
                            getFieldDecorator('rowIndex', {
                                rules: [{required: false}],
                                initialValue: objectInfo.rowIndex ? objectInfo.rowIndex : "0"
                            })(
                                <Input placeholder="请输入"/>
                            )}
                    </FormItem>
                    <FormItem style={{display: 'none'}}>
                        {
                            getFieldDecorator('productId', {
                                rules: [{required: false}],
                                initialValue: objectInfo.productId ? objectInfo.productId : ""
                            })(
                                <Input placeholder="请输入"/>
                            )}
                    </FormItem>

                    <Row className="infoForm_row">
                        <Col span="8">
                            <FormItem  {...formItemLayout} label="产品编码">
                                {
                                    getFieldDecorator('productCode', {
                                        rules: [{required: true, message: '请输入产品编码', whitespace: true}],
                                        initialValue: objectInfo.productCode ? objectInfo.productCode : ""
                                    })(
                                        <Input placeholder="请输入"/>
                                    )}
                            </FormItem>
                        </Col>

                        <Col span="8">
                            <FormItem  {...formItemLayout} label="中文名称">
                                {
                                    getFieldDecorator('productDescZh', {
                                        rules: [{required: true, message: '请输入中文名称', whitespace: true}],
                                        initialValue: objectInfo.productDescZh ? objectInfo.productDescZh : ""
                                    })(
                                        <Input placeholder="请输入"/>
                                    )}
                            </FormItem>
                        </Col>

                        <Col span="8">
                            <FormItem  {...formItemLayout} label="英文名称">
                                {
                                    getFieldDecorator('productDescEn', {
                                        //rules: [{ required: true, message: '请输入英文名称', whitespace: true }],
                                        initialValue: objectInfo.productDescEn ? objectInfo.productDescEn : ""
                                    })(
                                        <Input placeholder="请输入"/>
                                    )}
                            </FormItem>
                        </Col>
                    </Row>
                    <Row className="infoForm_row">
                        <Col span="7">
                            <FormItem  {...formItemLayout} label="序列号规则代码">
                                {
                                    getFieldDecorator('ruleCode', {
                                        //rules: [{ required: true, message: '请输入序列号规则代码', whitespace: true }],
                                        initialValue: objectInfo.ruleCode ? objectInfo.ruleCode : ""
                                    })(
                                        <Input placeholder="请输入" disabled="true"/>
                                    )}
                            </FormItem>
                        </Col>
                        <Col span="1">
                            <Icon type="search" style={{cursor:'pointer'}} onClick={this.ruleSearchClick}/>
                        </Col>

                        <Col span="8">
                            <FormItem  {...formItemLayout} label="旧物料号">
                                {
                                    getFieldDecorator('oldProductCode', {
                                        //rules: [{ required: true, message: '请输入旧物料号', whitespace: true }],
                                        initialValue: objectInfo.oldProductCode ? objectInfo.oldProductCode : ""
                                    })(
                                        <Input placeholder="请输入"/>
                                    )}
                            </FormItem>
                        </Col>

                        <Col span="8">
                            <FormItem  {...formItemLayout} label="散件物料号">
                                {
                                    getFieldDecorator('partsProductCode', {
                                        //rules: [{ required: true, message: '请输入散件物料号', whitespace: true }],
                                        initialValue: objectInfo.partsProductCode ? objectInfo.partsProductCode : ""
                                    })(
                                        <Input placeholder="请输入"/>
                                    )}
                            </FormItem>
                        </Col>
                    </Row>
                    <Row className="infoForm_row">
                        <Col span="8">
                            <FormItem  {...formItemLayout} label="69码">
                                {
                                    getFieldDecorator('skuBarcode', {
                                        //rules: [{ required: true, message: '请输入69码', whitespace: true }],
                                        initialValue: objectInfo.skuBarcode ? objectInfo.skuBarcode : ""
                                    })(
                                        <Input placeholder="请输入"/>
                                    )}
                            </FormItem>
                        </Col>

                        <Col span="8">
                            <FormItem  {...formItemLayout} label="长(cm)">

                                {
                                    getFieldDecorator('length', {
                                        rules: [{required: false, message: '请输入长', whitespace: true}],
                                        initialValue: objectInfo.length ? String(objectInfo.length) : ""
                                    })(
                                        <Input placeholder="请输入"/>
                                    )}
                            </FormItem>
                        </Col>

                        <Col span="8">
                            <FormItem  {...formItemLayout} label="宽(cm)">
                                {
                                    getFieldDecorator('width', {
                                        rules: [{required: false, message: '请输入宽', whitespace: true}],
                                        initialValue: objectInfo.width ? String(objectInfo.width) : ""
                                    })(
                                        <Input placeholder="请输入"/>
                                    )}
                            </FormItem>
                        </Col>
                    </Row>
                    <Row className="infoForm_row">
                        <Col span="8">
                            <FormItem  {...formItemLayout} label="高(cm)">
                                {
                                    getFieldDecorator('height', {
                                        rules: [{required: false, message: '请输入高', whitespace: true}],
                                        initialValue: objectInfo.height ? String(objectInfo.height) : ""
                                    })(
                                        <Input placeholder="请输入"/>
                                    )}
                            </FormItem>
                        </Col>

                        <Col span="8">
                            <FormItem  {...formItemLayout} label="毛重(kg)">
                                {
                                    getFieldDecorator('grossWeight', {
                                        rules: [{required: false, message: '请输入毛重', whitespace: true}],
                                        initialValue: objectInfo.grossWeight ? String(objectInfo.grossWeight) : ""
                                    })(
                                        <Input placeholder="请输入"/>
                                    )}
                            </FormItem>
                        </Col>

                        <Col span="8">
                            <FormItem  {...formItemLayout} label="净重(kg)">
                                {
                                    getFieldDecorator('netWeight', {
                                        rules: [{required: false, message: '请输入净重', whitespace: true}],
                                        initialValue: objectInfo.netWeight ? String(objectInfo.netWeight) : ""
                                    })(
                                        <Input placeholder="请输入"/>
                                    )}
                            </FormItem>
                        </Col>
                    </Row>
                    <Row className="infoForm_row">
                        <Col span="8">
                            <FormItem  {...formItemLayout} label="皮重(kg)">
                                {
                                    getFieldDecorator('tareWeight', {
                                        rules: [{required: false, message: '请输入皮重', whitespace: true}],
                                        initialValue: objectInfo.tareWeight ? String(objectInfo.tareWeight) : ""
                                    })(
                                        <Input placeholder="请输入"/>
                                    )}
                            </FormItem>
                        </Col>
                        <Col span="8">
                            <FormItem  {...formItemLayout} label="体积(cm³)">
                                {
                                    getFieldDecorator('volume', {
                                        rules: [{required: false, message: '体积', whitespace: true}],
                                        initialValue: objectInfo.volume ? String(objectInfo.volume) : ""
                                    })(
                                        <Input placeholder="请输入"/>
                                    )}
                            </FormItem>
                        </Col>

                        <Col span="8">
                            <FormItem  {...formItemLayout} label="单价">
                                {
                                    getFieldDecorator('unitPrice', {
                                        rules: [{required: false, message: '请输入单价', whitespace: true}],
                                        initialValue: objectInfo.unitPrice ? String(objectInfo.unitPrice) : ""
                                    })(
                                        <Input placeholder="请输入"/>
                                    )}
                            </FormItem>
                        </Col>
                    </Row>
                    <Row className="infoForm_row">
                        <Col span="8">
                            <FormItem  {...formItemLayout} label="产品组">
                                {
                                    getFieldDecorator('productGroup', {
                                        rules: [{required: false, message: '请输入产品组', whitespace: true}],
                                        initialValue: objectInfo.productGroup ? objectInfo.productGroup : ""
                                    })(
                                        <Input placeholder="请输入"/>
                                    )}
                            </FormItem>
                        </Col>

                        <Col span="8">
                            <FormItem  {...formItemLayout} label="用户自定义">
                                {
                                    getFieldDecorator('userDefined1', {
                                        //rules: [{ required: true, message: '请输入用户自定义1', whitespace: true }],
                                        initialValue: objectInfo.userDefined1 ? objectInfo.userDefined1 : ""
                                    })(
                                        <Input placeholder="请输入"/>
                                    )}
                            </FormItem>
                        </Col>
                    </Row>
                    {/*操作按钮*/}
                    {actionButtons}
                </Form>

                <SerialRuleSetModal getTableData={this.setRuleFormData} ref='serialRuleSetModal'/>
            </div>
        );
    }
}
